﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GBHNawaData.Dao
{
    public class MaterialDao
    {
        //public IList<vmsa140_product_basic> GetAllMaterial()
        //{
        //    using (var context = new MISDBEntities())
        //    {
        //        var material = (from m in context.vmsa140_product_basic
        //                       where m.mflagdel != "x"
        //                       select m).ToList();

        //        return material;
        //    }
        //}

        public IList<vmsa140_product_basic> GetAllMaterialByIds(string[] mmatnos)
        {
            using (var context = new GBHNawaDbEntities())
            {
                var material = (from m in context.vmsa140_product_basic
                                where m.mflagdel != "x" && mmatnos.Contains(m.mmatno)
                                select m).ToList();

                return material;
            }
        }

        //public IList<vmsa140_product_basic> GetAllMaterialByMeanCode(string[] mMeanCodes)
        //{
        //    using (var context = new MISDBEntities())
        //    {
        //        var material = (from m in context.vmsa140_product_basic
        //                        where m.mflagdel != "x" && mMeanCodes.Contains(m.meancode)
        //                        select m).ToList();

        //        return material;
        //    }
        //}

        public IList<vmsa140_product_basic> SearchMaterial(string matno, string matName, string matNameEng, string barcode)
        {
            using (var context = new GBHNawaDbEntities())
            {
                var material = (from m in context.vmsa140_product_basic
                                where m.mflagdel != "x"
                                select m);

                if (!string.IsNullOrEmpty(matno))
                {
                    material = material.Where(m => m.mmatno.Contains(matno));
                }

                if (!string.IsNullOrEmpty(matName))
                {
                    material = material.Where(m => m.mmatthai.Contains(matName));
                }

                if (!string.IsNullOrEmpty(matNameEng))
                {
                    material = material.Where(m => m.mmateng.Contains(matNameEng));
                }

                if (!string.IsNullOrEmpty(barcode))
                {
                    material = material.Where(m => m.meancode.Contains(barcode));
                }

                if (material == null)
                {
                    return new List<vmsa140_product_basic>();
                }
                return material.ToList();
            }
        }

        public vmsa140_product_basic GetMaterialByMatNo(string Matno)
        {
            using (var context = new GBHNawaDbEntities())
            {
                var material = (from m in context.vmsa140_product_basic
                                where m.mflagdel != "x" && m.mmatno == Matno
                                select m).FirstOrDefault();

                return material;
            }
        }

        //public vmsa140_product_basic GetMaterialByNpiBarcode(string NpiBarcode)
        //{
        //    using (var context = new MISDBEntities())
        //    {
        //        var material = (from m in context.vmsa140_product_basic
        //                        where m.mflagdel != "x" && m.meancode == NpiBarcode
        //                        select m).FirstOrDefault();

        //        return material;
        //    }
        //}

        public vmsa144_product_convunit GetConvertUnit(string NawaMatno)
        {
            using (var context = new GBHNawaDbEntities())
            {
                //var material = (from m in context.vmsa144_product_convunit
                //                join mm in context.vmsa140_product_basic on m.mmatno equals mm.mmatno
                //                where mm.meancode == NawaMatno && m.muom.ToUpper() == "BD"
                //                select m);
                var material = (from m in context.vmsa144_product_convunit
                                where m.mmatno == NawaMatno && ( m.muom.ToUpper() == "BD" ||  m.muom.ToUpper() == "BOX")
                                select m);
                var materialfirst =      material.FirstOrDefault();

                return materialfirst;
            }
        }

        
    }
}
